package com.taoyibao.hongcanting.ui.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.event.EventFinishActivity;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.ui.mine.delegate.SetPayPwdStepTwoDelegate;
import com.taoyibao.hongcanting.utils.DataUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * @author TangAnna
 * @description: 设置支付密码第二部
 * @date :${DATA} 17:07
 */
public class SetPayPwdStepTwoActivity extends BaseActivityPresenter<SetPayPwdStepTwoDelegate> implements View.OnClickListener {

    @Override
    protected Class<SetPayPwdStepTwoDelegate> getDelegateClass() {
        return SetPayPwdStepTwoDelegate.class;
    }

    /**
     * @param context
     * @param title   不同的业务的title
     */
    public static void open(Context context, String title) {
        Intent intent = new Intent(context, SetPayPwdStepTwoActivity.class);
        intent.putExtra(CodeConstant.TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setTitle(getIntent().getStringExtra(CodeConstant.TITLE));
        viewDelegate.get(R.id.btn_setPayPwd2_confirm).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_setPayPwd2_confirm://下一步
                if (viewDelegate.canSubmitPwd()) {
                    setPwd();
                }
                break;
        }
    }

    /**
     * 设置支付密码
     */
    public void setPwd() {
        String pwdMD5 = DataUtils.md5(DataUtils.md5(viewDelegate.getInputPwd() + CodeConstant.SET_PAY_PWD_MD5));
        JsonApiWrapper.getApiService().setPayPwd(SpUtils.getToken(), SpUtils.getMemberId(), pwdMD5)
                .compose(new DefaultTransformer<CompanyBaseModel>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel companyBaseModel) {
                        if (companyBaseModel.code == NetCode.HTTP_200) {
                            ToastUtils.showToast("设置成功！");
                            sendSetPayPwdSuccess();
                        } else {
                            super.onNext(companyBaseModel);
                        }

                    }
                });
    }

    /**
     * 发送消息设置密码成功
     */
    public void sendSetPayPwdSuccess() {
        EventFinishActivity finishActivity = new EventFinishActivity();
        finishActivity.setPayPedSuccess = true;
        EventBus.getDefault().post(finishActivity);
        finish();
    }
}
